import { Logger } from "./Logger";

export class WebUtil {

    public static webUrlData: Object = {};
    public static t: number = 0;

    public static init() {
        window.addEventListener('message', (event) => {
            Logger.info("onJsMsg", event.data.toString())
            let data: string = event.data;
            this.callH5()
        });
    }

    // http://localhost:3000/m/productlist.html?search=%E9%9E%8B&price=1&num=2
    // 根据url中参数名获取值
    // ?search=%E9%9E%8B&price=1&num=2 参数
    // 参数名 = search  参数值 = 鞋
    // 参数名 = price  参数值 = 1
    // 参数名 = num  参数值 = 2
    public static getQueryString(name: string) {
        // console.log(location);
        // console.log(location.search);
        // 1. 获取整个url参数的值的字符串 去掉第一个字符串 按照&分割成一个数组
        let urlParams: string[] = location.search.substr(1).split('&');
        for (let i = 0; i < urlParams.length; i++) {
            // search=%E9%9E%8B  =前面参数名 以=分割 如果等号前面的和参数名一样 返回=号后面的参数的值
            if (name == urlParams[i].split('=')[0]) {
                // 返回当前参数的值 同时转码
                return decodeURIComponent(urlParams[i].split('=')[1]);
            }
        }
    }

    public static callH5() {
        // "https://secure.example.net");
        // let sdfd: Uint8Array = Uint8ArrayHelper.stringToUint8Array("hahasujiehao")
        // Logger.log("callH5====2====", sdfd)

        let obj = {}
        obj["jhao"] = "adfwterer"
        obj["test"] = "alkhlwketjre"
        let str = JSON.stringify(obj);
        Logger.log("callH5", str)
        window.parent.postMessage(str, "*")
    }

}
